
#include <QXmlStreamReader>
#include <QFile>

#include "TrueLoveClient.h"

TrueLoveClient::TrueLoveClient()
{}

TrueLoveClient::TrueLoveClient(QString pNom, QString pPrenom, int pAge, QString pAdress, QString pEmail, 
			QList<QString> pHobbies, QList<QString> pObjectifs, QString pSexeRecherche,
			int pDiffAge, int pApparence, int pCharme, int pHumour, int pConversation,
			int pIntelligence, int pGentillesse, QString pUrlPhoto, QString pSexe)
{
	nom = pNom;
	prenom = pPrenom;
	age = pAge;
	adress = pAdress;
	email = pEmail;
	hobbies = pHobbies;
	objectifs = pObjectifs;
	sexeRecherche = pSexeRecherche;
	diffAge = pDiffAge;
	apparence = pApparence;
	charme = pCharme;
	humour = pHumour;
	conversation = pConversation;
	intelligence = pIntelligence;
	gentillesse = pGentillesse;
	urlPhoto = pUrlPhoto;
	sexe = pSexe;
}


QList<TrueLoveClient> TrueLoveClient::GetClients()
{
	QList<TrueLoveClient> clients;
	QFile file("xml/clients.xml");
	
	if(file.open(QIODevice::ReadOnly | QIODevice::Text))
	{
		QXmlStreamReader reader(&file);
		while(!reader.atEnd())
		{
			QXmlStreamReader::TokenType token = reader.readNext();
			
			if(token == QXmlStreamReader::StartDocument)
				continue; // ignore le header, inutile certes.	

			if(token == QXmlStreamReader::StartElement)
				clients.append(ParseClient(reader));	
		}

		file.close();
		reader.clear();
	}

	return clients;
}

TrueLoveClient TrueLoveClient::ParseClient(QXmlStreamReader& xml)
{
	TrueLoveClient c;	// to be filled all along this function

	xml.readNext();
	while(!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "client"))
	{
		QXmlStreamReader::TokenType token = xml.readNext();
		
		if(token == QXmlStreamReader::EndElement)
			continue;

		if(xml.name() == "id")
		{
			xml.readNext();
			c.SetId(xml.text().toString().toInt());
		}

		if(xml.name() == "nom") 
		{
			xml.readNext();
			c.SetNom(xml.text().toString());
		}

		if(xml.name() == "prenom")	
		{	
			xml.readNext();
			c.SetPrenom(xml.text().toString());
		}

		if(xml.name() == "age")
		{
			xml.readNext();
			c.SetAge(xml.text().toString().toInt());
		}
		
		if(xml.name() == "sexe")
		{
			xml.readNext();
			c.SetSexe(xml.text().toString());
		}

		if(xml.name() == "adress")
		{
			xml.readNext();
			c.SetAdress(xml.text().toString());
		}

		if(xml.name() == "email")
		{
			xml.readNext();
			c.SetEmail(xml.text().toString());
		}

		if(xml.name() == "hobby")
		{
			xml.readNext();
			c.AddHobby(xml.text().toString());
		}

		if(xml.name() == "objectif")
		{
			xml.readNext();
			c.AddObjectif(xml.text().toString());
		}

		if(xml.name() == "sexeRecherche")
		{
			xml.readNext();
			c.SetSexeRecherche(xml.text().toString());
		}

		if(xml.name() == "diffAge")
		{
			xml.readNext();
			c.SetDiffAge(xml.text().toString().toInt());
		}

		if(xml.name() == "apparence")
		{
			xml.readNext();
			c.SetApparence(xml.text().toString().toInt());
		}

		if(xml.name() == "charme")
		{
			xml.readNext();
			c.SetCharme(xml.text().toString().toInt());
		}

		if(xml.name() == "humour")
		{
			xml.readNext();
			c.SetHumour(xml.text().toString().toInt());
		}

		if(xml.name() == "intelligence")
		{
			xml.readNext();
			c.SetIntelligence(xml.text().toString().toInt());
		}

		if(xml.name() == "gentillesse")
		{
			xml.readNext();
			c.SetGentillesse(xml.text().toString().toInt());
		}

		if(xml.name() == "conversation")
		{
			xml.readNext();
			c.SetConversation(xml.text().toString().toInt());
		}

		if(xml.name() == "urlPhoto")
		{
			xml.readNext();
			c.SetUrlPhoto(xml.text().toString());
		}
	}
	
	return c;
}

void TrueLoveClient::printClient()
{
	printf("_\n |Id: %d\n |Nom: %s\n |Prénom: %s\n |Age: %d\n |Adresse: %s\n |Email: %s\n |Hobbies: ",
		GetId(), GetNom().toStdString().c_str(), GetPrenom().toStdString().c_str(), GetAge(), 
			GetAdress().toStdString().c_str(), GetEmail().toStdString().c_str());
	
	for(int i=0; i<GetHobbies().size(); i++) 
		printf("%s ", GetHobbies().at(i).toStdString().c_str());

	printf("\n |Objectifs: ");
	for(int i=0; i<GetObjectifs().size(); i++) 
		printf("%s ", GetObjectifs().at(i).toStdString().c_str());
	
	printf("\n |Sexe recherché: %s\n |Différence age: %d\n |Apparence: %d\n |Charme: %d\n |Humour: %d\n |",
		GetSexeRecherche().toStdString().c_str(), GetDiffAge(), GetApparence(), 
			GetCharme(), GetHumour());
	printf("Conversation: %d\n |Intelligence: %d\n |Gentillesse: %d\n |Url photo: %s\n |Sexe: %s\n\n",
		GetConversation(), GetIntelligence(), GetGentillesse(), 
			GetUrlPhoto().toStdString().c_str(), GetSexe().toStdString().c_str());
}

TrueLoveClient TrueLoveClient::FindClientById(QList<TrueLoveClient> list, int id)
{
	TrueLoveClient c;

	for(int i=0; i<list.size(); i++)
	{
		c = list.at(i);
		if(c.GetId() == id) return list.at(i);
	}

	return c;
}

TrueLoveClient::~TrueLoveClient()
{

}

